/*
 * @(#)DataHandlerProxy.java $version 2012-3-2
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nadostar.extensions.data;

import java.util.ArrayList;
import java.util.List;

import com.nadostar.extensions.util.Reloadable;

/**
 * @author LEESUNG
 * 
 */
public class DataHandlerProxy extends AbstractDataHandler {
	private DataHandler dataHandler;

	/**
	 * Instantiates a new data handler proxy.
	 * 
	 * @param dataHandler
	 *            the data handler
	 */
	public DataHandlerProxy(DataHandler dataHandler) {
		this.dataHandler = dataHandler;
	}

	protected void initialize() {
		if (dataHandler instanceof Reloadable) {
			((Reloadable) dataHandler).load();
		}
	}

	public void destroy() {
		dataHandler.destroy();
	}

	public boolean containsKey(String key) {
		return dataHandler.containsKey(key);
	}

	public String evaluate(String key, boolean resolve) {
		return dataHandler.evaluate(key, resolve);
	}

	public String getRootName() {
		return dataHandler.getRootName();
	}

	public List<String> getValues(String key) {
		return dataHandler.getValues(key);
	}

	public List<DataHandler> selectAll(String key) throws DataHandlerException {
		return wrap(dataHandler.selectAll(key));
	}

	public DataHandler peek(String key) {
		DataHandler subDataHander = dataHandler.peek(key);
		return subDataHander != null ? wrap(subDataHander) : null;
	}

	public DataHandler select(String key) throws DataHandlerException {
		return wrap(dataHandler.select(key));
	}

	public List<DataHandler> peekAll(String path) {
		return wrap(dataHandler.peekAll(path));
	}

	public List<DataHandler> peekChildren() {
		return wrap(dataHandler.peekChildren());
	}

	/**
	 * Wrap.
	 * 
	 * @param handler
	 *            the handler
	 * 
	 * @return the data handler
	 */
	protected DataHandler wrap(DataHandler handler) {
		return new DataHandlerProxy(handler);
	}

	/**
	 * Wrap.
	 * 
	 * @param handlers
	 *            the handlers
	 * 
	 * @return the list< data handler>
	 */
	protected List<DataHandler> wrap(List<DataHandler> handlers) {
		List<DataHandler> wrapper = new ArrayList<DataHandler>(handlers.size());

		for (DataHandler handler : handlers) {
			wrapper.add(new DataHandlerProxy(handler));
		}

		return wrapper;
	}
}
